﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyToHelp.Business;
using EasyToHelp.Entity;
using EasyToHelp.Service;
using Microsoft.AspNetCore.Mvc;

namespace EasyToHelp.WAP.Controllers
{
    /// <summary>
    /// 商品
    /// </summary>
    public class GoodsController : BaseController
    {
        private readonly IGoodsClassService GdClass;
        private readonly IShopService ShopService;
        private readonly IMerchantInfoService Merchant;
        private readonly IGoodsListService Goods;
        public GoodsController(IGoodsClassService _GoodsClass, IShopService _ShopService, IMerchantInfoService _Merchant, IGoodsListService _Goods)
        {
            GdClass = _GoodsClass;
            ShopService = _ShopService;
            Merchant = _Merchant;
            Goods = _Goods;
        }
        #region 页面
        /// <summary>
        /// 商品首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        public IActionResult GoodsClass()
        {
            return View();
        }
        /// <summary>
        /// 商品详细
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult GoodsDetails(int Id)
        {
            return View();
        }
        public IActionResult GoodsEdit(int Id)
        {
            var list = GdClass.GetList().Where(x => x.PId == 0).ToList();
            list.ForEach(x =>
            {
                x.GdClass = GdClass.GetList().Where(a => a.PId == x.Id).ToList();
            });
            ViewBag.GoodsClass = list;
            return View();
        }
        /// <summary>
        /// 商户商品管理
        /// </summary>
        /// <returns></returns>
        public IActionResult GoodsManage()
        {
            var list = GdClass.GetList().Where(x => x.PId == 0).ToList();
            list.ForEach(x =>
            {
                x.GdClass = GdClass.GetList().Where(a => a.PId == x.Id).ToList();
            });
            ViewBag.GoodsClass = list;
            return View();
        }
        /// <summary>
        /// 商铺类别
        /// </summary>
        /// <returns></returns>
        public IActionResult FlourishingClass()
        {
            return View();
        }
        /// <summary>
        /// 商铺列表
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopList()
        {
            return View();
        }
        /// <summary>
        /// 商铺管理
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopManage()
        {
            ViewBag.Market = MarketBusiness.GetMarket();
            ViewBag.GoodsClass = GdClass.GetList().Where(x => x.PId == 0);
            var model = ShopService.GetList().Where(x => x.MerchantId == Merchant.GetLicenset(UserId()).MerchantId).FirstOrDefault();
            return View(model);
        }
        /// <summary>
        /// 购物车
        /// </summary>
        /// <returns></returns>
        public IActionResult Cart()
        {
            return View();
        }
        /// <summary>
        /// 商品收藏
        /// </summary>
        /// <returns></returns>
        public IActionResult GoodsCollection()
        {
            return View();
        }
        /// <summary>
        /// 店铺收藏
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopCollection()
        {
            return View();
        }
        #endregion

        #region 方法
        public IActionResult EditShop(ShopLeaseInfo model)
        {
            if (model.ShopName.Length == 0)
            {
                return Json(ResultInfo.Error("请填写商铺名称！"));
            }
            if (model.ShopLogo.Length == 0)
            {
                return Json(ResultInfo.Error("请上传店招图片！"));
            }
            if (model.ShopQRCode.Length == 0)
            {
                return Json(ResultInfo.Error("请上传二维码！"));
            }
            if (model.ShopAddress.Length == 0)
            {
                return Json(ResultInfo.Error("请输入店铺详细地址！"));
            }
            if (model.Description.Length == 0)
            {
                return Json(ResultInfo.Error("请输入商品详细介绍！"));
            }
            model.BusinessLicenseId = Merchant.GetLicenset(UserId()).Id;
            var MerchantInfo = Merchant.GetList().Where(x => x.UserId == UserId()).FirstOrDefault();
            if (MerchantInfo == null)
            {
                return Json(ResultInfo.Error("请先完善商户基本信息！"));
            }
            else
            {
                model.MerchantId = MerchantInfo.Id;
            }
           
            if (ShopService.Edit(model) > 0)
            {
                return Json(ResultInfo.Success("编辑成功！"));
            }
            return Json(ResultInfo.Error("编辑失败！"));
        }

        public IActionResult EditGoods(GoodsList model)
        {
            if (model.Id > 0)
            {
                if (Goods.Update(model) > 0)
                {
                    return Json(ResultInfo.Success("更新成功！"));
                }
                else
                {
                    return Json(ResultInfo.Error("更新失败！"));
                }
            }
            else
            {
                if (Goods.Add(model) > 0)
                {
                    return Json(ResultInfo.Success("添加成功！"));
                }
                else
                {
                    return Json(ResultInfo.Error("添加失败！"));
                }
            }
        }
        #endregion

    }
}


